define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facrm/order/index' + location.search,
                    add_url: 'facrm/order/add',
                    edit_url: 'facrm/order/edit',
                    del_url: 'facrm/order/del',
                    multi_url: 'facrm/order/multi',
                    import_url: 'facrm/order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                 pageList: [10,100,999],columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'ordersn', title: __('Ordersn'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'admin.nickname', title: __('销售名'), operate: 'LIKE'},
                        {field: 'customer.name', title: __('Customer.name'), operate: 'LIKE'},
                        {field: 'customer.mobile', title: __('Customer.mobile'), operate: 'LIKE'},
                        {field: 'product.name', title: __('Product.name'), operate: 'LIKE'},
                        {field: 'product.image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'number', title: __('Number')},
                        {field: 'oldmoney', title: __('原价'), operate:false},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'desc', title: __('Desc'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {field: 'timestamp', title: __('Timestamp'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'address', title: __('地址')},
                        {field: 'comeway', title: __('来源')},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('编辑'),
                                    title: __('编辑'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'facrm/order/edit',
                                    callback: function (data, ret) {
                                        location.reload();
                                    },

                                    visible: function (row) {
                                        if(row.utypes!=1){
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'ajax',
                                    text: __('删除'),
                                    title: __('删除'),
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    url: 'facrm/order/del',
                                    confirm: '确认删除?',
                                    success: function (data, ret) {
                                        location.reload();
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        return false;
                                    },
                                    visible: function (row) {
                                        if(row.status==1){
                                            return false;
                                        }else{
                                            return true;
                                        }
                                    }
                                }

                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
